<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *  News Model Class  
 *  
 * @package XGO CMS v2.0 
 * @subpackage News  
 * @author Manh Nguyen - 31/07/2012 
 * @link http://sunsoft.vn
 */
class Group_model extends DataMapper {

    var $model = 'group_model';
    var $table = 'groups';

    // --------------------------------------------------------------------
    // Construct
    // --------------------------------------------------------------------
    function __construct($id = NULL) {
        parent::__construct($id);
    }

    // --------------------------------------------------------------------
    // Relationships
    // --------------------------------------------------------------------

    var $has_many = array(
        'user' => array(
            'class' => 'user_model',
            'other_field' => 'group'
        ),
        'role' => array(
            'class' => 'role_model',
            'other_field' => 'group'
        )
    );
    // --------------------------------------------------------------------
    var $has_one = array();
    // --------------------------------------------------------------------
    // Validation
    // --------------------------------------------------------------------	

    var $validation = array(
        'title' => array('required', 'trim', 'max_length' => 100),
        'filename' => array('required', 'trim'),
        'create' => array('required', 'trim'),
    );
    // --------------------------------------------------------------------
    /**
     * Default order in query select 
     */
    var $default_order_by = array('id' => 'desc');

    // --------------------------------------------------------------------
    /**
     * string value of object
     * 
     * @access private
     * @param null
     * @return string 
     */
    function __toString() {
        return empty($this->title) ? '---empty---' : $this->title;
    }

    public function get_list_role_by_group_id($group_id) {
        //get group by id
        $this->where('id', $group_id)->get();
        //get roles by group id
        $this->role->select('title');
        $list_role = $this->role->get();

        $role = new Role_model();
        $list_role_by_id = array();
        $i = 0;
        foreach ($list_role as $role) {
            $list_role_by_id[$i] = $role->title;
            $i++;
        }
        return $list_role_by_id;
    }

    /**
     * 
     * @param type $limit
     * @return \Group_model
     */
    function get_list($limit) {
        $group = new Group_model();
        $group->select('id', 'title', 'filename');
        $group->limit($limit);
        $group->get();
        return $group;
    }

    /**
     * array metadata for generate jquery grid.
     *
     * @access public
     * @param null
     * @return array
     */
    function jqgrid_metadata() {
        //edittype: 'text', 'textarea', 'select', 'checkbox', 'password', 'button', 'image', 'file', 'custom'
        return array(
            'names' => array(
                'id',
                $this->localize_label('title'),
                $this->localize_label('role')
            ),
            'model' => array(
                array('name' => 'id',
                    'hidden' => TRUE,
                    'key' => TRUE,
                    'editrules' => array('edithidden' => TRUE)
                ),
                array('name' => 'title',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                ),
                array('name' => 'role',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                )
            )
        );
    }

    // --------------------------------------------------------------------
    /**
     * rewite delete function to check relative object before delete. 
     * 
     * @access public
     * @param obj, string
     * @return -1 or bool
     */
    function delete($object = '', $related_field = '') {
        if (empty($object) && !is_array($object) && $this->id > 0) {
            if ($this->user->count() > 0) {
                return -1;
            }
        }
        return parent::delete($object, $related_field);
    }
    
    function total(){
    	$this->select("count(id) as total");
    	$this->get();
    
    	return array(
    			"title" => $this->lang->line('group'),
    			"total"	=> $this->total,
    	);
    }

}

/* End of file news_model.php */
/* Location: ./application/models/news_model.php */